package org.deng.vertx.web.dao;

import java.time.LocalDateTime;

import org.deng.vertx.common.util.DateUtils;
import org.deng.vertx.web.entity.User;
import org.deng.vertx.web.model.AsResult;

import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer;

/**
 * @Author: DengLibin
 * @Date: Create in 2022-04-29 16:58:49
 * @Description: 
 */
public class UserDao {
    private MySqlDao mySqlDao;
    private UserDao(){}
    private static class Inner {
        public static final UserDao userDao = new UserDao();
    }

    public static UserDao getInstance(){
        UserDao dao =  Inner.userDao;
        dao.mySqlDao = MySqlDao.getInstance();
        return dao;
    }

    /**
     * @Author: DengLibin
     * @Date: Create in 2022-04-29 17:48:40
     * @Description: 添加
     *
     * 
     * @param user
     * @param consumer
     */
    public void insert(User user, Consumer<AsResult<User>> consumer){
        String sql = "insert into user(id, username, age, create_date) values(?, ?, ?, ?)";
        user.setCreateDate(LocalDateTime.now());
        Object[] params = {user.getId(), user.getUsername(), user.getAge(), DateUtils.formatDate(user.getCreateDate())};
        mySqlDao.insert(sql, params, asResult -> {
            consumer.accept(AsResult.success(user));
        });
    }

}
